﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class ProductEditControl : YuanlinUserControl,
        IReplyConverter<ProductReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;//商品名称（常用名）
        protected ValuedTextBox aliasValuedTextBox;//别名
        protected FileUpload attachmentData;//商品图片
        protected ValuedTextBox latinNameValuedTextBox;//拉丁名
        protected ValuedDropDownList categoryValuedDropDownList;//植物分类
        protected CheckBoxList applicationCategoryCheckBoxList;//应用分类
        protected IButton SaveButton;
        protected IButton BackButton;
        private Product product;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetProductRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            //植物分类
            ProductCategoryListRequest request = new ProductCategoryListRequest();
            request.CategoryType = 0;
            EntityList categories = BusinessManager.GetProductCategoryList(RequestHelper.BuildGetProductCategoryListRequest(request));

            ProductCategory topCategory = new ProductCategory();
            topCategory.Title.Value = "<选择植物分类>";
            topCategory.EntityId.Value = -1;
            categories.Insert(0, topCategory);

            this.categoryValuedDropDownList.DataSource = categories;
            this.categoryValuedDropDownList.DataTextField = "Title";
            this.categoryValuedDropDownList.DataValueField = "EntityId";
            this.categoryValuedDropDownList.DataBind();

            //应用分类
            request = new ProductCategoryListRequest();
            request.CategoryType = 1;
            EntityList applicationCategories = BusinessManager.GetProductCategoryList(RequestHelper.BuildGetProductCategoryListRequest(request));
            this.applicationCategoryCheckBoxList.DataSource = applicationCategories;
            this.applicationCategoryCheckBoxList.DataTextField = "Title";
            this.applicationCategoryCheckBoxList.DataValueField = "EntityId";
            this.applicationCategoryCheckBoxList.DataBind();

            if (product != null)
            {
                titleValuedTextBox.Value = product.Title.Value;
                aliasValuedTextBox.Value = product.Alias.Value;
                latinNameValuedTextBox.Value = product.LatinName.Value;
                categoryValuedDropDownList.Value = product.CategoryId.Value.ToString();

                SiteHuamuAndAppCategoryRelationListRequest request2 = new SiteHuamuAndAppCategoryRelationListRequest();
                request2.HuamuId = product.EntityId.Value;
                EntityList siteHuamuAndAppCategoryRelationList = BusinessManager.GetSiteHuamuAndAppCategoryRelationList(RequestHelper.BuildGetSiteHuamuAndAppCategoryRelationListRequest(request2));
                if (siteHuamuAndAppCategoryRelationList.Count == 0)
                    return;

                ListItem item = null;
                foreach (SiteHuamuAndAppCategoryRelation siteHuamuAndAppCategoryRelation in siteHuamuAndAppCategoryRelationList)
                {
                    item = this.applicationCategoryCheckBoxList.Items.FindByValue(siteHuamuAndAppCategoryRelation.CategoryId.Value.ToString());
                    if (item != null)
                    {
                        item.Selected = true;
                    }
                }
            }
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (product != null)
            {
                product.Title.Value = CensorWordManager.CensorWord(titleValuedTextBox.Value);
                product.Alias.Value = CensorWordManager.CensorWord(aliasValuedTextBox.Value);
                product.LatinName.Value = CensorWordManager.CensorWord(latinNameValuedTextBox.Value);
                product.QueryValue.Value = string.Format("、{0}、{1}", product.Title.Value, product.Alias.Value);
                product.CategoryId.Value = Int32.Parse(categoryValuedDropDownList.Value);

                if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
                {
                    SiteUrls.Instance().DeleteFile(SiteUrls.Instance().GetFile(product));
                    product.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);
                    SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(product), attachmentData);
                }
                BusinessManager.UpdateProduct(product);

                //创建花木与应用分类的关系
                SiteHuamuAndAppCategoryRelation siteHuamuAndAppCategoryRelation = new SiteHuamuAndAppCategoryRelation();
                siteHuamuAndAppCategoryRelation.EntityId.Value = 1;
                siteHuamuAndAppCategoryRelation.HuamuId.Value = product.EntityId.Value;
                foreach (ListItem item in this.applicationCategoryCheckBoxList.Items)
                {
                    if (item.Selected)
                    {
                        siteHuamuAndAppCategoryRelation.CategoryIds += "," + Globals.ChangeType<int>(item.Value);
                    }
                }
                if (!string.IsNullOrEmpty(siteHuamuAndAppCategoryRelation.CategoryIds))
                {
                    siteHuamuAndAppCategoryRelation.CategoryIds = siteHuamuAndAppCategoryRelation.CategoryIds.Substring(1);
                    BusinessManager.UpdateSiteHuamuAndAppCategoryRelation(siteHuamuAndAppCategoryRelation);
                }
            }

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetProductListUrl(string.Empty, string.Empty));
        }

        #region IReplyConverter<ProductReply,GetOperation> Members

        public void ConvertReply(ProductReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                product = reply.Entity as Product;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}
